package com.zmn.oms.api.controller.third.sogou;

import com.alibaba.fastjson.JSON;
import com.zmn.common.utils.number.NumberUtil;
import com.zmn.common.utils.string.StringUtil;
import com.zmn.oms.common.exception.OmsBaseException;
import com.zmn.oms.third.sogou.SogouConsts;
import com.zmn.oms.third.sogou.dto.SogouClueData;
import com.zmn.oms.third.sogou.service.SogouClueService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 类描述：
 *
 * @author xiewenbing
 * @date 2021/04/27 17:30
 */
@RestController
@RequestMapping("/sogou/api")
@Slf4j
public class SogouClueController {

    @Autowired
    private SogouClueService sogouClueService;

    @PostMapping("/clue/{channelId}")
    public String channelPullClue(@PathVariable(name="channelId") Integer channelId,
                                  @RequestBody String clueContent) {

        if (StringUtil.isBlank(clueContent) || NumberUtil.isNullOrZero(channelId)) {
            log.info("【{}】接受线索信息 - 参数错误", SogouConsts.CHANNEL_NAME);

            return "fail";
        }

        log.info("【{}】接受线索信息 - 【{}】【{}】", SogouConsts.CHANNEL_NAME, channelId, clueContent);

        try {
            List<SogouClueData> sogouClueDataList = JSON.parseArray(clueContent, SogouClueData.class);
            sogouClueService.handleClueList(channelId,sogouClueDataList);
        } catch (Exception exception) {
            log.info("【{}】处理线索失败 - 【{}】【{}】", SogouConsts.CHANNEL_NAME, exception.getMessage(),exception);
            return "fail";
        }

        return "success";
    }

}
